﻿using System.Linq;
using MyAlbum.Data.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using BlogML.Xml;
using System;
using System.Collections.Generic;
using System.IO;
using MyAlbum.Services;
using MyAlbum.Model;

namespace UnitTests
{


    /// <summary>
    ///This is a test class for BlogRepsitoryTest and is intended
    ///to contain all BlogRepsitoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class BlogRepsitoryTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for BlogRepsitory Constructor
        ///</summary>
        [TestMethod()]
        public void BlogRepsitoryConstructorTest()
        {
            string rootDirectory = @"c:\";
            Directory.Delete("c:\\blogs");
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            Assert.IsTrue(Directory.Exists("c:\\blogs"));
            Directory.Delete("c:\\blogs");
        }

        [TestMethod()]
        public void TestGetCategories()
        {
            string rootDirectory = "c:\\tests";
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            MyAlbum.Utility.IValidationSummary s = null;
            var catName = Guid.NewGuid().ToString("N");
            var service = new BlogService(target, new WebUser(new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity("tal"), new[] { "Admin" })));
            var p = new BlogMLPost()
            {
                Title = Guid.NewGuid().ToString(),
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() }
            };
            p.Categories.Add(new BlogMLCategoryReference() { Ref = catName });
            p.Categories.Add(new BlogMLCategoryReference() { Ref = catName });
            p.Categories.Add(new BlogMLCategoryReference() { Ref = catName + "1" });
            string postId = service.AddPost(p, new Uri("http://localhost"), s);
            var categories = target.GetCategories();
            Assert.IsNotNull(categories);
            var t = categories.SingleOrDefault(n => n.Name == catName);
            Assert.IsNotNull(t);
            Assert.AreEqual(2, t.Count);

            t = categories.SingleOrDefault(n => n.Name == catName+"1");
            Assert.IsNotNull(t);
            Assert.AreEqual(1, t.Count);
        }

        /// <summary>
        ///A test for AddComment
        ///</summary>
        [TestMethod()]
        public void AddCommentTest()
        {

            string rootDirectory = "c:\\tests";
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            MyAlbum.Utility.IValidationSummary s = null;
            var service = new BlogService(target, new WebUser(new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity("tal"), new[] { "Admin" })));
            string postId = service.AddPost(new BlogMLPost()
            {
                Title = Guid.NewGuid().ToString(),
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() }
            }, new Uri("http://localhost"), s);
            BlogMLComment comment = new BlogMLComment()
            {
                Approved = true,
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() },
                Title = Guid.NewGuid().ToString(),
                DateCreated = DateTime.UtcNow,
                UserEMail = Guid.NewGuid().ToString(),
                UserName = Guid.NewGuid().ToString(),
                UserUrl = Guid.NewGuid().ToString()
            };
            target.AddComment(postId, comment);
            var post = service.GetPost(postId);
            Assert.IsNotNull(post.Comments);
            Assert.AreEqual(1, post.Comments.Count);
            var loaded = post.Comments[0];
            Assert.AreEqual(comment.Approved, loaded.Approved);
            Assert.AreEqual(comment.Content.Text, loaded.Content.Text);
            Assert.AreEqual(comment.DateCreated, loaded.DateCreated);
            Assert.AreEqual(comment.ID, loaded.ID);
            Assert.AreEqual(comment.Title, loaded.Title);
            Assert.AreEqual(comment.UserEMail, loaded.UserEMail);
            Assert.AreEqual(comment.UserName, loaded.UserName);
            Assert.AreEqual(comment.UserUrl, loaded.UserUrl);
        }

        /// <summary>
        ///A test for AddPost
        ///</summary>
        [TestMethod()]
        public void AddPostTest()
        {
            string rootDirectory = "c:\\tests";
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            BlogMLPost post = new BlogMLPost()
            {
                PostName = Guid.NewGuid().ToString(),
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() },
                Approved = true,
                DateCreated = DateTime.UtcNow,
                ID = Guid.NewGuid().ToString("N"),
                Views = 3,
                Title = Guid.NewGuid().ToString(),
                PostUrl = Guid.NewGuid().ToString(),
                PostType = BlogML.BlogPostTypes.Normal

            };

            var id = target.AddPost(post);
            var loaded = target.GetPost(id);
            Assert.IsNotNull(loaded);
            Assert.AreEqual(post.PostName, loaded.PostName);
            Assert.AreEqual(post.Content.Text, loaded.Content.Text);
            Assert.AreEqual(post.Approved, loaded.Approved);
            Assert.AreEqual(post.DateCreated, loaded.DateCreated);
            Assert.AreEqual(post.ID, loaded.ID);
            Assert.AreEqual(post.Views, loaded.Views);
            Assert.AreEqual(post.Title, loaded.Title);
            Assert.AreEqual(post.PostUrl, loaded.PostUrl);
            Assert.AreEqual(post.PostType, loaded.PostType);
        }

        /// <summary>
        ///A test for DeleteComment
        ///</summary>
        [TestMethod()]
        public void DeleteCommentTest()
        {
            string rootDirectory = "c:\\tests";
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            MyAlbum.Utility.IValidationSummary s = null;
            var service = new BlogService(target, new WebUser(new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity("tal"), new[] { "Admin" })));
            string postId = service.AddPost(new BlogMLPost()
            {
                PostName = Guid.NewGuid().ToString(),
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() }
            }, new Uri("http://localhost"), s);
            BlogMLComment comment = new BlogMLComment()
            {
                Approved = true,
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() },
                Title = Guid.NewGuid().ToString(),
                DateCreated = DateTime.UtcNow,
                UserEMail = Guid.NewGuid().ToString(),
                UserName = Guid.NewGuid().ToString(),
                UserUrl = Guid.NewGuid().ToString(),
                ID = Guid.NewGuid().ToString()
            };
            target.AddComment(postId, comment);
            target.DeleteComment(postId, comment.ID);
            var post = service.GetPost(postId);
            Assert.IsNotNull(post.Comments);
            Assert.AreEqual(0, post.Comments.Count);
        }

        /// <summary>
        ///A test for DeletePost
        ///</summary>
        [TestMethod()]
        public void DeletePostTest()
        {
            string rootDirectory = "c:\\tests";
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            BlogMLPost post = new BlogMLPost()
            {
                PostName = Guid.NewGuid().ToString(),
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() },
                Approved = true,
                DateCreated = DateTime.UtcNow,
                ID = Guid.NewGuid().ToString("N"),
                Views = 3,
                Title = Guid.NewGuid().ToString(),
                PostUrl = Guid.NewGuid().ToString(),
                PostType = BlogML.BlogPostTypes.Normal

            };

            var id = target.AddPost(post);
            var loaded = target.GetPost(id);
            Assert.IsNotNull(loaded);
            target.DeletePost(id);
            loaded = target.GetPost(id);
            Assert.IsNull(loaded);
        }

        /// <summary>
        ///A test for GetFileName
        ///</summary>
        [TestMethod()]
        [DeploymentItem("PersonalAlbumModel.dll")]
        public void GetFileNameTest()
        {
            string rootDirectory = "c:\\tests";
            PrivateObject param0 = new PrivateObject(new BlogRepsitory(rootDirectory));
            BlogRepsitory_Accessor target = new BlogRepsitory_Accessor(param0);
            DateTime dt = new DateTime(1999, 3, 3);
            string id = Guid.NewGuid().ToString("N");
            string expected = dt.ToString(BlogRepsitory_Accessor.DateFormat) + "_" + id + ".xml";
            string actual;
            actual = target.GetFileName(dt, id);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for GetPost
        ///</summary>
        [TestMethod()]
        public void GetPostTest()
        {
            string rootDirectory = "c:\\tests";
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            BlogMLPost post = new BlogMLPost()
            {
                PostName = Guid.NewGuid().ToString(),
                Content = new BlogMLContent() { Text = Guid.NewGuid().ToString() },
                Approved = true,
                DateCreated = DateTime.UtcNow,
                ID = Guid.NewGuid().ToString("N"),
                Views = 3,
                Title = Guid.NewGuid().ToString(),
                PostUrl = Guid.NewGuid().ToString(),
                PostType = BlogML.BlogPostTypes.Normal

            };

            var id = target.AddPost(post);
            var loaded = target.GetPost(id);
            Assert.IsNotNull(loaded);
            Assert.AreEqual(post.PostName, loaded.PostName);
            Assert.AreEqual(post.Content.Text, loaded.Content.Text);
            Assert.AreEqual(post.Approved, loaded.Approved);
            Assert.AreEqual(post.DateCreated, loaded.DateCreated);
            Assert.AreEqual(post.ID, loaded.ID);
            Assert.AreEqual(post.Views, loaded.Views);
            Assert.AreEqual(post.Title, loaded.Title);
            Assert.AreEqual(post.PostUrl, loaded.PostUrl);
            Assert.AreEqual(post.PostType, loaded.PostType);
        }

        [TestMethod()]
        public void Get_Non_Exising_Post_Should_Return_Null()
        {
            string rootDirectory = "c:\\tests";
            BlogRepsitory target = new BlogRepsitory(rootDirectory);
            var result = target.GetPost("aaa");
            Assert.IsNull(result);
        }

        /// <summary>
        ///A test for GetPosts
        ///</summary>
        [TestMethod()]
        public void GetPostsTest2()
        {
            string rootDirectory = string.Empty; // TODO: Initialize to an appropriate value
            BlogRepsitory target = new BlogRepsitory(rootDirectory); // TODO: Initialize to an appropriate value
            BlogMLCategory tags = null; // TODO: Initialize to an appropriate value
            Nullable<DateTime> sinceDate = new Nullable<DateTime>(); // TODO: Initialize to an appropriate value
            int maxCount = 0; // TODO: Initialize to an appropriate value
            IEnumerable<BlogMLPost> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<BlogMLPost> actual;
            actual = target.GetPosts(tags, sinceDate, maxCount);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetPosts
        ///</summary>
        [TestMethod()]
        public void GetPostsTest1()
        {
            string rootDirectory = string.Empty; // TODO: Initialize to an appropriate value
            BlogRepsitory target = new BlogRepsitory(rootDirectory); // TODO: Initialize to an appropriate value
            string searchFilter = string.Empty; // TODO: Initialize to an appropriate value
            Nullable<DateTime> sinceDate = new Nullable<DateTime>(); // TODO: Initialize to an appropriate value
            int maxCount = 0; // TODO: Initialize to an appropriate value
            IEnumerable<BlogMLPost> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<BlogMLPost> actual;
            actual = target.GetPosts(searchFilter, sinceDate, maxCount);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetPosts
        ///</summary>
        [TestMethod()]
        public void GetPostsTest()
        {
            string rootDirectory = string.Empty; // TODO: Initialize to an appropriate value
            BlogRepsitory target = new BlogRepsitory(rootDirectory); // TODO: Initialize to an appropriate value
            int pageIndex = 0; // TODO: Initialize to an appropriate value
            int pageSize = 0; // TODO: Initialize to an appropriate value
            Nullable<DateTime> sinceDate = new Nullable<DateTime>(); // TODO: Initialize to an appropriate value
            IEnumerable<BlogMLPost> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<BlogMLPost> actual;
            actual = target.GetPosts(pageIndex, pageSize, sinceDate);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for ParseDate
        ///</summary>
        [TestMethod()]
        [DeploymentItem("PersonalAlbumModel.dll")]
        public void ParseDateTest()
        {
            PrivateObject param0 = null; // TODO: Initialize to an appropriate value
            BlogRepsitory_Accessor target = new BlogRepsitory_Accessor(param0); // TODO: Initialize to an appropriate value
            string fileName = string.Empty; // TODO: Initialize to an appropriate value
            DateTime expected = new DateTime(); // TODO: Initialize to an appropriate value
            DateTime actual;
            actual = target.ParseDate(fileName);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for Serialize
        ///</summary>
        [TestMethod()]
        [DeploymentItem("PersonalAlbumModel.dll")]
        public void SerializeTest()
        {
            PrivateObject param0 = null; // TODO: Initialize to an appropriate value
            BlogRepsitory_Accessor target = new BlogRepsitory_Accessor(param0); // TODO: Initialize to an appropriate value
            string fileName = string.Empty; // TODO: Initialize to an appropriate value
            BlogMLPost post = null; // TODO: Initialize to an appropriate value
            target.Serialize(fileName, post);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for UpdateComment
        ///</summary>
        [TestMethod()]
        public void UpdateCommentTest()
        {
            string rootDirectory = string.Empty; // TODO: Initialize to an appropriate value
            BlogRepsitory target = new BlogRepsitory(rootDirectory); // TODO: Initialize to an appropriate value
            string postId = string.Empty; // TODO: Initialize to an appropriate value
            BlogMLComment comment = null; // TODO: Initialize to an appropriate value
            target.UpdateComment(postId, comment);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for UpdatePost
        ///</summary>
        [TestMethod()]
        public void UpdatePostTest()
        {
            string rootDirectory = string.Empty; // TODO: Initialize to an appropriate value
            BlogRepsitory target = new BlogRepsitory(rootDirectory); // TODO: Initialize to an appropriate value
            BlogMLPost post = null; // TODO: Initialize to an appropriate value
            target.UpdatePost(post);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for Serializer
        ///</summary>
        [TestMethod()]
        [DeploymentItem("PersonalAlbumModel.dll")]
        public void SerializerTest()
        {
            PrivateObject param0 = null; // TODO: Initialize to an appropriate value
            BlogRepsitory_Accessor target = new BlogRepsitory_Accessor(param0); // TODO: Initialize to an appropriate value
            XmlSerializer actual;
            actual = target.Serializer;
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
    }
}
